import {Component, OnInit} from '@angular/core';
import {STColumn, STColumnButton} from '@delon/abc';
import {SFSchema} from '@delon/form';
import {NzMessageService} from 'ng-zorro-antd';
import {BaseService} from '@sb/base';
import {SysCommandService} from '../service/sys-command.service';
import {SysCommand} from '../entity/sys-command';

@Component({
    selector: 'sys-command',
    templateUrl: './sys-command.component.html',
    styleUrls: ['./sys-command.component.less'],
    providers: [
        {
            provide: BaseService,
            useClass: SysCommandService
        }
    ]
})
export class SysCommandComponent implements OnInit {

    logForCommandId = '';

    commandType = '';

    constructor(
        private message: NzMessageService) {
    }

    commandSendPanelVisible = false;
    currentCommandId = '';
    currentCommand = '';

    customOperateButtons: STColumnButton[] = [{
        text: '发送',
        icon: 'none',
        type: 'none',
        click: (record, modal, comp) => {
            this.showCommandSendPanel(record);
        }
    }, {
        text: '日志',
        icon: 'none',
        type: 'none',
        click: (record, modal, comp) => {
            this.logForCommandId = record.id;
        }
    }];
    columns: STColumn[] = [
        {
            title: '',
            type: 'checkbox',
            index: 'id'
        }, {
            title: '名称',
            index: 'commandName',
            width: '10%',
        }, {
            title: '命令',
            index: 'command',
            className: 'word-wrap'
        }, {
            title: '周期表达式',
            index: 'cron',
            width: '120px'
        }, {
            title: '启用',
            index: 'running',
            width: '60px'
        }, {
            title: '最大生存时间',
            index: 'maxAlive',
            className: 'text-center',
            width: '120px'
        }, {
            title: '最后修改时间',
            index: 'lastModifyTime',
            className: 'text-center',
            width: '180px'
        }, {
            title: '操作区',
            index: 'operate',
            className: 'text-center',
            width: '250px'
        }
    ];

    schema: SFSchema = {
        properties: {
            commandName: {
                type: 'string',
                title: '命令名称',
                minLength: 2
            },
            command: {
                type: 'string',
                title: '命令',
                ui: {
                    widget: 'textarea',
                    autosize: {minRows: 6, maxRows: 20}
                }
            },
            cron: {
                type: 'string',
                title: '周期表达式'
            },
            running: {
                type: 'boolean',
                title: '启用'
            },
            maxAlive: {
                type: 'integer',
                title: '最大生存时间（S）',
                default: 120,
                minimum: 0
            }
        },
        ui: {
            spanLabelFixed: 160
        },
        required: ['commandName', 'command']
    };

    sendCommand2() {
        this.currentCommandId = '';
        this.currentCommand = '';
        this.commandSendPanelVisible = true;
        this.commandType = 'bash';
    }

    ngOnInit(): void {
    }

    showCommandSendPanel(record: SysCommand) {
        this.currentCommandId = record.id;
        this.currentCommand = record.command;
        this.commandSendPanelVisible = true;
        this.commandType = '';
    }
}
